package com.csy.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.csy.auth.domain.SysPermission;
import com.csy.auth.domain.SysUser;
import com.csy.auth.mapper.PermissionMapper;
import com.csy.auth.mapper.UserMapper;
import com.csy.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<SysUser> sysUsers = userMapper.selectList(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, s));
        if(CollectionUtils.isEmpty(sysUsers))return null;
        SysUser user = sysUsers.get(0);
        List<SysPermission>permissions =permissionMapper.findByUid(user.getId());
        user.setPermissions(permissions);
        return sysUsers.get(0);
    }
}
